/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFContatos.java
 *
 * Created on 04/12/2012, 12:21:22
 */
package contatospro;

import java.awt.Component;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Junnyor S2 Rafa
 */
public class JFContatos extends JPanel {
    
    public JFContatos() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("contatosproPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Contatos c");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        pessoasQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery.getResultList();
        contatos1 = new contatospro.Contatos();
        situacoesQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT s FROM Situacoes s");
        situacoesList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : situacoesQuery.getResultList();
        situacoesQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT s FROM Situacoes s");
        situacoesList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : situacoesQuery1.getResultList();
        tiposContatosQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT t FROM TiposContatos t");
        tiposContatosList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tiposContatosQuery.getResultList();
        pessoasQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery1.getResultList();
        contatosQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Contatos c");
        contatosList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : contatosQuery.getResultList();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        codPessoaLabel = new javax.swing.JLabel();
        codSituacaoLabel = new javax.swing.JLabel();
        codTipoContatoLabel = new javax.swing.JLabel();
        interesseClienteLabel = new javax.swing.JLabel();
        dataNovoContatoLabel = new javax.swing.JLabel();
        horaNovoContatoLabel = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        newButton1 = new javax.swing.JButton();
        saveButton1 = new javax.swing.JButton();
        refreshButton1 = new javax.swing.JButton();
        deleteButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        buscarjButton = new javax.swing.JButton();
        situacaojComboBox1 = new javax.swing.JComboBox();
        pessoajComboBox1 = new javax.swing.JComboBox();
        pessoajComboBox = new javax.swing.JComboBox();
        situacoesjButton1 = new javax.swing.JButton();
        situacaojComboBox = new javax.swing.JComboBox();
        situacoesjButton = new javax.swing.JButton();
        tipocontatojComboBox = new javax.swing.JComboBox();
        tipocontatojButton = new javax.swing.JButton();
        datacontatojFormattedTextField = new javax.swing.JFormattedTextField();
        horacontatojFormattedTextField = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        interessejTextArea = new javax.swing.JTextArea();

        FormListener formListener = new FormListener();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codContato}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("codContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codPessoa}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("codPessoa"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codSituacao}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("codSituacao"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codTipoContato}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("codTipoContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.dataNovoContato}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("dataNovoContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.horaNovoContato}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("horaNovoContato"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.interesseCliente}"), contatos1, org.jdesktop.beansbinding.BeanProperty.create("interesseCliente"));
        bindingGroup.addBinding(binding);

        setMinimumSize(new java.awt.Dimension(500, 480));
        setPreferredSize(new java.awt.Dimension(850, 495));

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable, "");
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codContato}"));
        columnBinding.setColumnName("Cod Contato");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codPessoa.pessoa}"));
        columnBinding.setColumnName("Pessoa");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codSituacao.situacao}"));
        columnBinding.setColumnName("Situacao");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);

        codPessoaLabel.setText("Pessoa:");

        codSituacaoLabel.setText("Situacao:");

        codTipoContatoLabel.setText("Tipo de Contato:");

        interesseClienteLabel.setText("Interesse do Cliente:");

        dataNovoContatoLabel.setText("Data do Novo Contato:");

        horaNovoContatoLabel.setText("Hora do Novo Contato:");

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jToolBar1.setName("jToolBar1"); // NOI18N

        newButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Novo6.png"))); // NOI18N
        newButton1.setText("Novo");
        newButton1.setToolTipText("Novo Cadastro");
        newButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        newButton1.setName("newButton1"); // NOI18N
        newButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        newButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newButton1.addActionListener(formListener);
        jToolBar1.add(newButton1);

        saveButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Salvar1.png"))); // NOI18N
        saveButton1.setText("Salvar");
        saveButton1.setToolTipText("Salvar Contato");
        saveButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        saveButton1.setName("saveButton1"); // NOI18N
        saveButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        saveButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton1.addActionListener(formListener);
        jToolBar1.add(saveButton1);

        refreshButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Atualizar1.png"))); // NOI18N
        refreshButton1.setText("Atualizar");
        refreshButton1.setToolTipText("Atualizar Tela");
        refreshButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refreshButton1.setMaximumSize(new java.awt.Dimension(65, 60));
        refreshButton1.setName("refreshButton1"); // NOI18N
        refreshButton1.setPreferredSize(new java.awt.Dimension(65, 60));
        refreshButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        refreshButton1.addActionListener(formListener);
        jToolBar1.add(refreshButton1);

        deleteButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Excluir3.png"))); // NOI18N
        deleteButton1.setText("Excluir");
        deleteButton1.setToolTipText("Excluir Contato");
        deleteButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteButton1.setMaximumSize(new java.awt.Dimension(50, 60));
        deleteButton1.setName("deleteButton1"); // NOI18N
        deleteButton1.setPreferredSize(new java.awt.Dimension(50, 60));
        deleteButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton1.addActionListener(formListener);
        jToolBar1.add(deleteButton1);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(730, 69));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Pessoa");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Situação");
        jLabel2.setName("jLabel2"); // NOI18N

        buscarjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/lupa1.png"))); // NOI18N
        buscarjButton.setText("Buscar");
        buscarjButton.setName("buscarjButton"); // NOI18N
        buscarjButton.addActionListener(formListener);

        situacaojComboBox1.setName("situacaojComboBox1"); // NOI18N
        situacaojComboBox1.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Situacoes) {
                    Situacoes mec = (Situacoes)value;
                    setText(mec.getSituacao());
                }
                return this;
            }
        }
    );

    org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, situacoesList1, situacaojComboBox1);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${codSituacao}"), situacaojComboBox1, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    pessoajComboBox1.setName("pessoajComboBox1"); // NOI18N
    pessoajComboBox1.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Pessoas) {
                Pessoas mec = (Pessoas)value;
                setText(mec.getPessoa());
            }
            return this;
        }
    }
    );

    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pessoasList1, pessoajComboBox1);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${codPessoa}"), pessoajComboBox1, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(40, 40, 40)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(pessoajComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(37, 37, 37)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(situacaojComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(41, 41, 41)
                    .addComponent(buscarjButton))
                .addComponent(jLabel2))
            .addGap(63, 63, 63))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(12, 12, 12)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(situacaojComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(buscarjButton)
                .addComponent(pessoajComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(12, Short.MAX_VALUE))
    );

    pessoajComboBox.setName("pessoajComboBox"); // NOI18N
    pessoajComboBox.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Pessoas) {
                Pessoas mec = (Pessoas)value;
                setText(mec.getPessoa());
            }
            return this;
        }
    }
    );

    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pessoasList, pessoajComboBox);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${codPessoa}"), pessoajComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    situacoesjButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/lupa1.png"))); // NOI18N
    situacoesjButton1.setFocusable(false);
    situacoesjButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    situacoesjButton1.setName("situacoesjButton1"); // NOI18N
    situacoesjButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    situacoesjButton1.addActionListener(formListener);

    situacaojComboBox.setName("situacaojComboBox"); // NOI18N
    situacaojComboBox.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Situacoes) {
                Situacoes mec = (Situacoes)value;
                setText(mec.getSituacao());
            }
            return this;
        }
    }
    );

    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, situacoesList, situacaojComboBox);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${codSituacao}"), situacaojComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    situacoesjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/lupa1.png"))); // NOI18N
    situacoesjButton.setFocusable(false);
    situacoesjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    situacoesjButton.setName("situacoesjButton"); // NOI18N
    situacoesjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    situacoesjButton.addActionListener(formListener);

    tipocontatojComboBox.setName("tipocontatojComboBox"); // NOI18N
    tipocontatojComboBox.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof TiposContatos) {
                TiposContatos mec = (TiposContatos)value;
                setText(mec.getTipoContato());
            }
            return this;
        }
    }
    );

    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tiposContatosList, tipocontatojComboBox);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${codTipoContato}"), tipocontatojComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    tipocontatojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/lupa1.png"))); // NOI18N
    tipocontatojButton.setFocusable(false);
    tipocontatojButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    tipocontatojButton.setName("tipocontatojButton"); // NOI18N
    tipocontatojButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    tipocontatojButton.addActionListener(formListener);

    datacontatojFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
    datacontatojFormattedTextField.setName("datacontatojFormattedTextField"); // NOI18N

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${dataNovoContato}"), datacontatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
    bindingGroup.addBinding(binding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), datacontatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
    bindingGroup.addBinding(binding);

    horacontatojFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT))));
    horacontatojFormattedTextField.setName("horacontatojFormattedTextField"); // NOI18N

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${horaNovoContato}"), horacontatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
    bindingGroup.addBinding(binding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), horacontatojFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
    bindingGroup.addBinding(binding);

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    interessejTextArea.setColumns(20);
    interessejTextArea.setRows(5);
    interessejTextArea.setName("interessejTextArea"); // NOI18N

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contatos1, org.jdesktop.beansbinding.ELProperty.create("${interesseCliente}"), interessejTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"));
    bindingGroup.addBinding(binding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), interessejTextArea, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
    bindingGroup.addBinding(binding);

    jScrollPane1.setViewportView(interessejTextArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(codPessoaLabel)
                        .addComponent(codTipoContatoLabel))
                    .addGap(35, 35, 35)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(tipocontatojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(tipocontatojButton, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(6, 6, 6)
                            .addComponent(dataNovoContatoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(datacontatojFormattedTextField))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(pessoajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(situacoesjButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(codSituacaoLabel)
                        .addComponent(horaNovoContatoLabel))
                    .addGap(12, 12, 12)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(situacaojComboBox, 0, 142, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(situacoesjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(1, 1, 1))
                        .addComponent(horacontatojFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(interesseClienteLabel)
                    .addGap(15, 15, 15)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codPessoaLabel)
                    .addComponent(pessoajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(situacoesjButton)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codSituacaoLabel)
                    .addComponent(situacaojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(situacoesjButton1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(horacontatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(datacontatojFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataNovoContatoLabel)
                    .addComponent(horaNovoContatoLabel))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codTipoContatoLabel)
                    .addComponent(tipocontatojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(tipocontatojButton))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(interesseClienteLabel))
                .addGroup(layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(45, 45, 45))
    );

    bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == newButton1) {
                JFContatos.this.newButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton1) {
                JFContatos.this.saveButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton1) {
                JFContatos.this.refreshButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton1) {
                JFContatos.this.deleteButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == buscarjButton) {
                JFContatos.this.buscarjButtonActionPerformed(evt);
            }
            else if (evt.getSource() == situacoesjButton1) {
                JFContatos.this.situacoesjButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == situacoesjButton) {
                JFContatos.this.situacoesjButtonActionPerformed(evt);
            }
            else if (evt.getSource() == tipocontatojButton) {
                JFContatos.this.tipocontatojButtonActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    
            
private void newButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButton1ActionPerformed
        contatospro.Contatos c = new contatospro.Contatos();
        entityManager.persist(c);
        list.add(c);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        //Adicionar data do dia ao campo JPA
        Calendar calendario = Calendar.getInstance();
        contatos1.setDataNovoContato(calendario.getTime());  
        contatos1.setHoraNovoContato(calendario.getTime());

}//GEN-LAST:event_newButton1ActionPerformed

private void saveButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButton1ActionPerformed
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            JOptionPane.showMessageDialog(null, "Contato Cadastrado com Sucesso!!");

        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<contatospro.Contatos> merged = new ArrayList<contatospro.Contatos>(list.size());
            for (contatospro.Contatos c : list) {
                merged.add(entityManager.merge(c));
            }
            list.clear();
            list.addAll(merged);
             
            if ( (pessoajComboBox.getSelectedItem()== null)              ){
        JOptionPane.showMessageDialog(null,"O campo Pessoa é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }
             if ( (situacaojComboBox.getSelectedItem()== null)              ){
        JOptionPane.showMessageDialog(null,"O campo Situações é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }
              if ( (tipocontatojComboBox.getSelectedItem()== null)              ){
        JOptionPane.showMessageDialog(null,"O campo Tipo de Contato é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }
              if ( (interessejTextArea.getText().isEmpty())
              ){
        JOptionPane.showMessageDialog(null,"O campo Interesse do Cliente é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }
        }
}//GEN-LAST:event_saveButton1ActionPerformed

private void refreshButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButton1ActionPerformed
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
}//GEN-LAST:event_refreshButton1ActionPerformed

private void deleteButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButton1ActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<contatospro.Contatos> toRemove = new ArrayList<contatospro.Contatos>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            contatospro.Contatos c = list.get(masterTable.convertRowIndexToModel(selected[idx]));
        String nome = ("Deseja Realmente Excluir o Registro ?");  
        int opcao_escolhida = JOptionPane.showConfirmDialog(null,nome,"Excluir Registro ",JOptionPane.YES_NO_OPTION);  
        if (opcao_escolhida==JOptionPane.YES_OPTION){  
        toRemove.add(c);
        entityManager.remove(c);
        }
        else  return;
        JOptionPane.showMessageDialog(null, "Registro Excluído com Sucesso!!");
        }
        list.removeAll(toRemove);
}//GEN-LAST:event_deleteButton1ActionPerformed

private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
// TODO add your handling code here:
        if(pessoajComboBox1.getSelectedItem() != null){  
        query = entityManager.createNamedQuery("Contatos.findByCodPessoa");
        query.setParameter("codPessoa", pessoajComboBox1.getSelectedItem());
        //Salvar o resultado em uma coleção
        java.util.Collection data = query.getResultList();
        pessoajComboBox1.setSelectedItem(null);
        //Limpar a lista atual com os dados da grade antigos
        list.clear();
        //Adicionar o que armazenado na variave data com o novo resultado
        list.addAll(data);
                 
        } else {
        //Alterar a consulta da query do form, buscando a consulta findByPessoaLike
        query = entityManager.createNamedQuery("Contatos.findByCodSituacao");
        //Pegar o que foi escrito no campo pessoa adicionar um % para o like
        //e passar como parâmetro para a consulta
        query.setParameter("codSituacao", situacaojComboBox1.getSelectedItem());
        //Salvar o resultado em uma coleção
        java.util.Collection data = query.getResultList();
        situacaojComboBox1.setSelectedItem(null);

        //Limpar a lista atual com os dados da grade antigos
        list.clear();
        //Adicionar o que armazenado na variave data com o novo 
        list.addAll(data);        

         }
}//GEN-LAST:event_buscarjButtonActionPerformed

private void situacoesjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_situacoesjButton1ActionPerformed
// TODO add your handling code here:
       JFrame frame = new JFrame("Pessoas");
         frame.setContentPane(new JFPessoas());
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
}//GEN-LAST:event_situacoesjButton1ActionPerformed

private void situacoesjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_situacoesjButtonActionPerformed
// TODO add your handling code here:
       JFrame frame = new JFrame("Situacoes");
         frame.setContentPane(new JFSituacoes());
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
}//GEN-LAST:event_situacoesjButtonActionPerformed

private void tipocontatojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipocontatojButtonActionPerformed
// TODO add your handling code here:
       JFrame frame = new JFrame("Tipo Contato");
         frame.setContentPane(new JFTipoContato());
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
}//GEN-LAST:event_tipocontatojButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarjButton;
    private javax.swing.JLabel codPessoaLabel;
    private javax.swing.JLabel codSituacaoLabel;
    private javax.swing.JLabel codTipoContatoLabel;
    private contatospro.Contatos contatos1;
    private java.util.List<contatospro.Contatos> contatosList;
    private javax.persistence.Query contatosQuery;
    private javax.swing.JLabel dataNovoContatoLabel;
    private javax.swing.JFormattedTextField datacontatojFormattedTextField;
    private javax.swing.JButton deleteButton1;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel horaNovoContatoLabel;
    private javax.swing.JFormattedTextField horacontatojFormattedTextField;
    private javax.swing.JLabel interesseClienteLabel;
    private javax.swing.JTextArea interessejTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<contatospro.Contatos> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton1;
    private javax.swing.JComboBox pessoajComboBox;
    private javax.swing.JComboBox pessoajComboBox1;
    private java.util.List<contatospro.Pessoas> pessoasList;
    private java.util.List<contatospro.Pessoas> pessoasList1;
    private javax.persistence.Query pessoasQuery;
    private javax.persistence.Query pessoasQuery1;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton1;
    private javax.swing.JButton saveButton1;
    private javax.swing.JComboBox situacaojComboBox;
    private javax.swing.JComboBox situacaojComboBox1;
    private java.util.List<contatospro.Situacoes> situacoesList;
    private java.util.List<contatospro.Situacoes> situacoesList1;
    private javax.persistence.Query situacoesQuery;
    private javax.persistence.Query situacoesQuery1;
    private javax.swing.JButton situacoesjButton;
    private javax.swing.JButton situacoesjButton1;
    private javax.swing.JButton tipocontatojButton;
    private javax.swing.JComboBox tipocontatojComboBox;
    private java.util.List<contatospro.TiposContatos> tiposContatosList;
    private javax.persistence.Query tiposContatosQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFContatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFContatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFContatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFContatos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new JFContatos());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
